package com.tydic.borderer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tydic.borderer.domain.DeclarationForm;
import com.tydic.borderer.domain.bo.DeclarationFormQueryBo;
import com.tydic.borderer.mapper.DeclarationFormMapper;
import com.tydic.borderer.service.IDeclarationFormService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class DeclarationFormServiceImpl implements IDeclarationFormService {

    private final DeclarationFormMapper declarationFormMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(DeclarationForm declarationForm) {
        declarationFormMapper.insertOrUpdate(declarationForm);
    }

    @Override
    public Page<DeclarationForm> page(DeclarationFormQueryBo declarationFormQueryRequest) {
        LambdaQueryWrapper<DeclarationForm> lqw = Wrappers.lambdaQuery(DeclarationForm.class);
        lqw.orderByDesc(DeclarationForm::getCreateDate);
        return declarationFormMapper.selectPage(declarationFormQueryRequest.build(), lqw);
    }

    @Override
    public DeclarationForm get(String id) {
        return declarationFormMapper.selectById(id);
    }

    @Override
    public DeclarationForm getByDeclarationNumber(String declarationNumber) {
        return declarationFormMapper.selectOne(Wrappers.lambdaQuery(DeclarationForm.class).eq(DeclarationForm::getDeclarationNumber, declarationNumber), false);
    }
}
